package com.zdp.usercenter.auth;

import com.zdp.usercenter.security.MySecurityException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author sesshomaru
 * @date 2020/9/26 20:35
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionErrorHandler {

    @ExceptionHandler(MySecurityException.class)
    public ResponseEntity<ErrorBody> error(MySecurityException e){
        log.warn("发生MySecurityException异常:{}",e.getMessage());
        ResponseEntity<ErrorBody> responseEntity = new ResponseEntity<ErrorBody>(
                ErrorBody.builder()
                        .body("Token非法,用户不允许访问")
                        .status(HttpStatus.UNAUTHORIZED.value())
                        .build(),
                HttpStatus.UNAUTHORIZED
        );
        return responseEntity;
    }
}

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
class ErrorBody{
    private String body;
    private int status;
}
